package com.ws.haungjia.controller;

import com.ws.haungjia.Bean.User;
import com.ws.haungjia.Service.QiantaiyonghuServlet;
import com.ws.haungjia.utils.Res;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("qian")
public class Qianduanyonghuguanli {
    @Autowired
    QiantaiyonghuServlet qian;
    @RequestMapping("cha")
    public String chakna(Model model){
        List<User> cha = qian.cha();
        model.addAttribute("list",cha);
        return "WEB-INF/houtai/qianuser/list";
    }
    @RequestMapping("shan")
    public String shan(int id){
        qian.shan(id);
        return "redirect:/qian/cha";
    }
    @RequestMapping("/zengjia")
    public String zengjia(User user, MultipartFile touxinag) throws IOException {

        System.out.println(user);
        if (touxinag.getSize() > 0) {
            String replace = UUID.randomUUID().toString().replace("-", "");
            String fileName = replace + "." + touxinag.getOriginalFilename().split("\\.")[1];
            user.setTouxiang(fileName);
//            lastIndexo
            touxinag.transferTo(new File(Res.fileDir + fileName));
        }
        String s = DigestUtils.md5DigestAsHex(user.getPass().getBytes());
        user.setPass(s);
        int add = qian.zengjia(user.getName(), user.getPass(), user.getShouji(), user.getTouxiang(), new Date());
        if (add > 0) {
            return "redirect:/qian/cha";
        }else {
            return "qianduan/zhuce";
        }
    }

    @RequestMapping("/huixian")
    public String huixian(int id ,Model model) throws IOException {
        User huixian = qian.huixian(id);

        model.addAttribute("user",huixian);
        return "WEB-INF/houtai/qianuser/xiugai";
    }

    @RequestMapping("/xiugai")
    public String xiugai(String name, String pass, int shouji, int userID, MultipartFile touxaing) throws IOException {
        User user = new User();
        user.setName(name);
        user.setShouji(shouji);
        user.setUserID(userID);
        if (touxaing.getSize()>0){
            String replace = UUID.randomUUID().toString().replace("-", "");
            String s = replace + "." + touxaing.getOriginalFilename().split("\\.")[1];
            touxaing.transferTo(new File(Res.fileDir+s));
            user.setTouxiang(s);
        }else{
            user.setTouxiang("");
        }
        String s = DigestUtils.md5DigestAsHex(pass.getBytes());
        user.setPass(s);
        int xiugai = qian.xiugai(user);
        if (xiugai>0){
            return "redirect:/qian/cha";
        }else{
            return "WEB-INF/houtai/qianuser/xiugai";
        }
    }
}
